export namespace ZCC {
  export interface ListPageData<CM> {
    page: number,//当前page
    total: number,//总页数
    items: CM[],//list item
    loading: boolean,//用于刷新、加载中组件状态判断
    hasNextPage: boolean
  }

  export class BasePage {

    constructor(options: any) {
      this.initPage(options);
    }

    initPage(options: any) {
      options.onShareAppMessage = function () {
        const promise = new Promise(resolve => {
          setTimeout(() => {
            resolve({
              title: '一键查询企业多维数据，助您高效决策！',
              path: 'pages/launch/launch', // 分享路径
              imageUrl: '/static/images/mine/ic_share_cover.png',
            })
          }, 100)
        })
        return {
          title: '一键查询企业多维数据，助您高效决策！', // 分享标题
          path: 'pages/launch/launch', // 分享路径
          imageUrl: '/static/images/mine/ic_share_cover.png',
          promise
        }
      };
      return Page(options);
    }
  }
  // CM: list cell model
  export class RefreshListPage<CM> {
    presetData: ListPageData<CM>;
    constructor(options: any) {

      this.presetData = {
        page: 1,
        total: 0,
        items: [],
        loading: false,
        hasNextPage: true
      }
      options.data = {
        ...this.presetData,
        ...(options.data || {})
      }
      // 如果正在loading return;否则 重置page=1
      options.resetPageIfNeed = function () {
        if (this.data.loading) {
          // this.setData({
          // 	loading: false
          // })
          return false
        }
        // this.data.page = 1
        console.log('reset page 1')
        // wx.nextTick(() => {
        this.setData({
          page: 1
        })
        // })
        return true
      }
      //start loading
      options.setRefreshing = function () {
        if (!this.data.loading) {
          // this.data.loading = true
          // wx.nextTick(() => {
          this.setData({
            loading: true
          })
          // })
        }
      }
      //stop loading
      options.endRefreshing = function () {
        if (this.data.loading) {
          // this.data.loading = false

          // wx.nextTick(() => {
          this.setData({
            loading: false
          })
          // })
        }
      }

      //更新datasource ; newItems: 新加载的数据段
      options.updateDatasWithNewItems = function (newItems: CM[], total?: number, hasNextPage: boolean = true) {

        let tmp: CM[] = []
        let page = 1
        let _total = this.data.total || 0
        const block = newItems || []
        if (this.data.page === 1) {
          tmp = block
          if (block.length > 0) {
            ++page
            _total = total || 0
          } else {
            _total = 0
          }
        } else {
          tmp = this.data.items.concat(block)
          page = ++this.data.page
          console.log("this.data.page = " + this.data.page)
          if (block.length > 0) {
            _total = total || block.length
          }
        }

        // wx.nextTick(() => {
        this.setData({
          page: page,
          items: tmp,
          total: _total,
          hasNextPage: hasNextPage
        })
        // })
      }

      this.initPage(options)
    }

    initPage(options: any) {
      // options.onShareAppMessage = function () {
      // 	const promise = new Promise(resolve => {
      // 		setTimeout(() => {
      // 			resolve({
      // 				title: '一键查询企业多维数据，助您高效决策！',
      // 				path: '/pages/index/index', // 分享路径
      // 				imageUrl: '/static/images/mine/ic_share_cover.png',
      // 			})
      // 		}, 2000)
      // 	})
      // 	return {
      // 		title: '一键查询企业多维数据，助您高效决策！', // 分享标题
      // 		path: '/pages/index/index', // 分享路径
      // 		imageUrl: '/static/images/mine/ic_share_cover.png',
      // 		promise
      // 	}
      // };
      Page(options)
    }
  }
}

